<html>
<head>


<script type="text/javascript">
//A Print function for testing. Modify as desired for debug output.  
function print(string)
{
  document.getElementById("demo").innerHTML += "<p>" + string + "</p>";
}
function print_clear()
{
  document.getElementById("demo").innerHTML = "";
}
</script>


<script src="gps_baseclasses.js"></script>


<script type="text/javascript">
// Functions for test harness: 
function mainTest1()
{  
	print_clear();
	print("Simulation #1: Successful FIFO Scheduling of 2 resources.");  
	SIM = new Simulator(
		[new Process([ new Need("CPU",210,0), new Need("IO",440,0) ]), 
		 new Process([ new Need("CPU",620,0), new Need("IO",250,0)]), 
		 new Process([ new Need("CPU",430,0), new Need("IO",260,0)]), 
		 new Process([ new Need("CPU",250,0), new Need("IO",250,0)])], 
		[new Resource("CPU"), new Resource("IO")] );

}

function mainTest2()
{  	
	print_clear();
	print("Simulation #2: Deadlock with 2 resources.");  
  SIM = new Simulator(
    [new Process([ new Need("CPU",508,0),   new Need("IO",320,240) ]), 
		 new Process([ new Need("CPU",643,230), new Need("IO",407,123)]), 
		 new Process([ new Need("CPU",440,100),   new Need("IO",220,100)]), 
		 new Process([ new Need("CPU",250,0),   new Need("IO",250,0)])], 
    [new Resource("CPU"), new Resource("IO")] );

}

function mainTest3()
{  	
print_clear();
print("Simulation #3: Block-waiting test using CPU and LOCK.");  
  SIM = new Simulator(
    [new Process([ new Need("CPU",250,0),   new Need("LOCK",50,150) ]), 
	 new Process([ new Need("CPU",250,0),   new Need("LOCK",50,100)]), 
	 new Process([ new Need("CPU",250,0),   new Need("LOCK",50,50)]), 
	 new Process([ new Need("CPU",250,0),   new Need("LOCK",50,0)])], 
    [new Resource("CPU"), new Resource("IO"), new Resource("LOCK")] );
	
    SIM.getResource("LOCK").addBlockWaitFor("CPU");
}

function nextEvent()
{
   SIM.simNextEvent();
   print("-----------------------");
}

function previousEvent()
{
   SIM.rollBackToLastSnapshot();
   print("-----------------------");
}

function printMetrics()
{
  print("Active processes:");
  for(ii in SIM.processList){ p = SIM.processList[ii];
    print("P"+p.pID+": Response Time="+p.getFirstProgressTime()+", Waiting Time="+p.getWaitDuration()+
	  ", Running Time="+p.getRunDuration()+".");
  }
  print("Finished processes:");
  for(ii in SIM.terminatedProcList){ p = SIM.terminatedProcList[ii];
    print("P"+p.pID+": Response Time="+p.getFirstProgressTime()+", Waiting Time="+p.getWaitDuration()+
	  ", Running Time="+p.getRunDuration()+", Turn Around Time="+p.getTurnAroundTime()+".");
  }
  print("-----------------------");
}

</script>


<body>
<h1>Javascript Simulator Test</h1>
<p id="demo"> </p>

<button type="button" onclick="mainTest1()">Initialize Test 1</button>
<button type="button" onclick="mainTest2()">Initialize Test 2</button>
<button type="button" onclick="mainTest3()">Initialize Test 3</button>
<button type="button" onclick="nextEvent()">Next Event</button>
<button type="button" onclick="previousEvent()">Previous Event</button>
<button type="button" onclick="printMetrics()">Process Metrics</button>

</body>
</html> 
